package com.chief.hadoop.mr.combiner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.viewfs.ViewFileSystem;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

//统计每个手机号的总流量，有重复号码,并按照从大到小排序
public class FlowDriver {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration configuration = new Configuration();
        configuration.set("mapreduce.job.jar", "E:\\bigdata-start\\hadoop3\\hadoopCode\\target\\hadoopCode-1.0-SNAPSHOT.jar");
        configuration.set("mapreduce.app-submission.cross-platform", "true");

        Job job = Job.getInstance(configuration);
        job.setJarByClass(FlowDriver.class);

        job.setCombinerClass(FlowCombiner.class);

        job.setMapperClass(FlowMapper.class);
        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setReducerClass(FlowReduce.class);
        job.setOutputValueClass(FlowBean.class);
        job.setOutputValueClass(NullWritable.class);


        FileInputFormat.setInputPaths(job, new Path("/data1/combiner*.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/data1/combinerOut"));

        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }

    //upload
    @Test
    public void upload() throws URISyntaxException, IOException {
        FileSystem fileSystem = ViewFileSystem.newInstance(new URI("viewfs://mycluster/"), new Configuration());
        fileSystem.copyFromLocalFile(false, true, new Path("E:\\bigdata-start\\hadoop3\\hadoopCode\\src\\main\\java\\com\\chief\\hadoop\\mr\\combiner\\flow.txt"), new Path(("/data1/combiner1.txt")));
        fileSystem.copyFromLocalFile(false, true, new Path("E:\\bigdata-start\\hadoop3\\hadoopCode\\src\\main\\java\\com\\chief\\hadoop\\mr\\combiner\\flow.txt"), new Path(("/data1/combiner2.txt")));
        fileSystem.copyFromLocalFile(false, true, new Path("E:\\bigdata-start\\hadoop3\\hadoopCode\\src\\main\\java\\com\\chief\\hadoop\\mr\\combiner\\flow.txt"), new Path(("/data1/combiner3.txt")));
    }
}
